namespace _3 {

var lengthOfLongestSubstring = function(s: string): number {
    if (!s) return 0

    var answer = 1, tryAnotherAnswer = 1
    var chars = new Map<string, number>()
    chars.set(s[0], 0)
    for(var i = 1; i < s.length; i++) {
        if ( chars.has( s[i])) {
            i = chars.get(s[i]) + 1
            chars.clear()
            tryAnotherAnswer = 1
        }
        else {
            tryAnotherAnswer++;
        }
        chars.set( s[i], i)

        if (tryAnotherAnswer > answer) {
            answer = tryAnotherAnswer
        }
    }
    return answer
};

console.assert( lengthOfLongestSubstring( 'dvdf') === 3)
console.assert( lengthOfLongestSubstring( 'abcabcbb') === 3)
console.assert( lengthOfLongestSubstring( 'bbbbb') === 1)
console.assert( lengthOfLongestSubstring( 'pwwkew') === 3)

}